<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1100" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="8">
                    <el-form-item label="工艺编号" prop="route_code">
                        <el-input v-model="form.route_code" placeholder="请输入工艺编号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="工艺名称" prop="route_name">
                        <el-input v-model="form.route_name" placeholder="请输入工艺名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="工艺备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入工艺备注" clearable type="textarea"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="工序信息" prop="detail">
                <!-- <el-button type="primary" size="small" @click="selectProcess">选择工序</el-button> -->
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="300">
                    <el-table-column label="类型" align="center" prop="type">
                        <template #default="scope">
                            <el-select v-model="scope.row.type" placeholder="请选择" filterable>
                                <el-option label="工序" :value="1"></el-option>
                                <el-option label="工艺" :value="2"></el-option>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="工序/工艺路线" align="center" prop="working_route_id">
                        <template #default="scope">
                            <el-select v-model="scope.row.working_route_id" placeholder="请选择" filterable>
                                <el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="报工配比" align="center" prop="ratio">
                        <template #default="scope">
                            <el-input v-model="scope.row.ratio" placeholder="请输入报工配比" clearable></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="工序要求" align="center" prop="content">
                        <template #default="scope">
                            <el-input v-model="scope.row.content" placeholder="请输入工序要求" clearable></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="图纸" align="center" prop="drawing">
                        <template #default="scope">
                            <sc-upload height="50" width="50" v-model="scope.row.drawing"></sc-upload>
                        </template>
                    </el-table-column>
                    <el-table-column label="工序SOP文档" align="center" prop="sop">
                        <template #default="scope">
                            <sc-upload-file v-model="scope.row.sop" :limit="1">
                                <el-button type="primary" icon="el-icon-upload" size="small">选择文件</el-button>
                            </sc-upload-file>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <select-process v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></select-process>
</template>

<script>
import selectProcess from '../process/selectProcess.vue';
export default {
  components: { selectProcess },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增工艺',
				edit: '编辑工艺',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
            dialog: {
				save: false,
			},
			form: {
                id:"",
                work_name:"",
                remark:"",
                detail: [],
			},
			rules: {
                title: [
					{ required: true, message: '请输入工艺名称' }
				],
			},
            addTemplate: {},
            unitList:[],
		}
	},
	mounted() {
        // this.getUnit()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.goods.craft.edit.post(this.form);
					} else {
						res = await this.$API.goods.craft.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.goods.craft.detail.get({id:data.id});
            this.form.work_name = res.data.work_name
            this.form.remark = res.data.remark
            let array = []
            res.data.detail.map((item)=>{
                array.push({
                    working_id:item.working_id,
                    working_name:item.working.working_name,
                    remark:item.working.remark,
                })
            })
            this.form.detail = array
		},
        selectProcess(){
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        handleSuccess(data){
            let array = []
            data.map((item)=>{
                array.push({
                    working_id:item.id,
                    working_name:item.working_name,
                    remark:item.remark,
                })
            })
            let existingIds = this.form.detail.map(item => item.working_id);
            for(let i = array.length - 1; i >= 0; i--){
                if(existingIds.includes(array[i].working_id)){
                    array.splice(i, 1);
                }
            }
            this.form.detail = [...this.form.detail,...array]
        },
	}
}
</script>

<style>
    .el-input-group__append{
        padding: 0 8px;
    }
</style>
